### TABLES APPENDIX


## produces tables displayed in the appendix VI
# Table A VI.1, A VI.2, A VI.3, A VII.1


# Session info
# R version 4.1.0 (2021-05-18)
# Platform: x86_64-apple-darwin17.0 (64-bit)
# Running under: macOS 13.1


# Load packages -----------------------------------------------------------
# if needed install packages first:
#install.packages(c("tidyverse", "stargazer"))

library(tidyverse)
library(stargazer)

# Load data ---------------------------------------------------------------

rm(list = ls())

# load report-level data
data_rep_level <- read_csv("data/data_rep_level.csv")

### factorise IEG rating variable
data_rep_level$IEG_Outcome <- factor(data_rep_level$IEG_Outcome, 
                                     levels = c("Highly Unsatisfactory", 
                                                "Unsatisfactory",
                                                "Moderately Unsatisfactory",
                                                "Moderately Satisfactory",
                                                "Satisfactory",
                                                "Highly Satisfactory"))

# load UN data
meta_pred_un <- read_csv("data/meta_pred_un.csv")



# Robustness tests --------------------------------------------------------


########

### Table A VI.1

########

# positive assessment share and specific rating levels (factor variables)
model_iv_factor <- lm(sentiment ~ factor(IEG_Outcome), 
                      data_rep_level) 

model_iv_factor_type <- lm(sentiment ~ factor(IEG_Outcome) +
                             factor(type), 
                           data_rep_level)

model_iv_factor_type_fe <- lm(sentiment ~ factor(IEG_Outcome) + 
                                factor(type) +
                                factor(year) +
                                factor(country), 
                              data_rep_level)

# table output
model_output_factor <- list(model_iv_factor, model_iv_factor_type, model_iv_factor_type_fe)
stargazer(model_output_factor, type = "html", 
          out = "output/tableAVI1.htm", 
          keep.stat = c("n", "rsq", "adj.rsq"), 
          title = "Model Summary", 
          omit = c("country", "year"), ci = T,
          dep.var.labels = "Positive Assessment Share",
          intercept.bottom = T,
          covariate.labels = c("Unsatisfactory",
                               "Moderately Unsatisfactory Rating",
                               "Moderately Satisfactory Rating",
                               "Satisfactory Rating",
                               "Highly Satisfactory Rating",
                               "Report Type (PPAR)"
          ),
          add.lines=list(c('Year FE', 'No','No', 'Yes'), 
                         c('Country FE', 'No','No', 'Yes')),
          column.sep.width = "50pt"
)


########

### Table A VI.2

########

# models for restricted data after removing the lowest rating category 'Highly Unsatisfactory'
data_fe_restr <- data_rep_level %>% filter(IEG_Outcome != 'Highly Unsatisfactory')

model_iv_factor_restr <- lm(sentiment ~ factor(IEG_Outcome), data_fe_restr) 

model_iv_factor_type_restr <- lm(sentiment ~ factor(IEG_Outcome) +
                                   factor(type), 
                                 data_fe_restr)

model_iv_factor_type_fe_restr <- lm(sentiment ~ factor(IEG_Outcome) + 
                                      factor(type) +
                                      factor(year) +
                                      factor(country), 
                                    data_fe_restr)

# table output
model_output_restricted <- list(model_iv_factor_restr, model_iv_factor_type_restr,
                                model_iv_factor_type_fe_restr)
stargazer(model_output_restricted, type = "html", 
          out = "output/tableAVI2.htm", 
          keep.stat = c("n", "rsq", "adj.rsq"), 
          title = "Model Summary", 
          omit = c("country", "year"), ci = T,
          dep.var.labels = "Positive Assessment Share",
          intercept.bottom = T,
          covariate.labels = c("Moderately Unsatisfactory Rating",
                               "Moderately Satisfactory Rating",
                               "Satisfactory Rating",
                               "Highly Satisfactory Rating",
                               "Report Type (PPAR)"
          ),
          add.lines=list(c('Year FE', 'No','No', 'Yes'), 
                         c('Country FE', 'No','No', 'Yes')),
          column.sep.width = "50pt"
)


########

### Table A VI.3

########

# positive assessment share and general satisfaction rating
model_iv_gensat <- lm(sentiment ~ as.numeric(general_sat),
                      data_rep_level)

model_iv_gensat_type <- lm(sentiment ~ as.numeric(general_sat) + 
                             factor(type),
                           data_rep_level)

model_iv_gensat_type_fe <- lm(sentiment ~ as.numeric(general_sat) + 
                                factor(type) +
                                factor(country) +
                                factor(year),
                              data_rep_level)

# table output
model_output_gensat <- list(model_iv_gensat, model_iv_gensat_type, model_iv_gensat_type_fe)
stargazer(model_output_gensat, type = "html", 
          out = "output/tableAVI3.htm", 
          keep.stat = c("n", "rsq", "adj.rsq"), 
          title = "Model Summary", 
          omit = c("country", "year"), ci = T,
          dep.var.labels = "Positive Assessment Share",
          covariate.labels = c("IEG General Rating (Positive)",
                               "Report Type (PPAR)"
          ),
          intercept.bottom = T,
          add.lines=list(c('Year FE', 'No','No', 'Yes'), 
                         c('Country FE', 'No','No', 'Yes')),
          column.sep.width = "50pt")





########

### Table A VII.1

########


## calculate mean positive assessment share for each evaluation type
program_mean <- meta_pred_un[meta_pred_un$eval_type == 'Programme',]$sentiment
project_mean <- meta_pred_un[meta_pred_un$eval_type == 'Project',]$sentiment

# Welch Two Sample T-test
tt_out <- t.test(program_mean, project_mean)
